package com.ty.recyclerviewdemo

import android.graphics.Point
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.ty.recyclerviewdemo.bean.ItemBean
import com.ty.recyclerviewdemo.view.SpaceItemDecorator
import com.ty.recyclerviewdemo.view.SpanSize
import com.ty.recyclerviewdemo.view.SpannedGridLayoutManager
import com.ty.recyclerviewdemo.view.dragselectrecyclerview.DragSelectTouchListener
import com.ty.recyclerviewdemo.view.dragselectrecyclerview.DragSelectionProcessor
import kotlin.math.log

class MainActivity : AppCompatActivity() {
    val cancelTv by lazy { findViewById<TextView>(R.id.cancel) }
    val sureTv by lazy { findViewById<TextView>(R.id.sure) }
    val rlv by lazy { findViewById<RecyclerView>(R.id.rlv) }
    var datas = arrayListOf<ItemBean>()
    private var mDragSelectTouchListener: DragSelectTouchListener? = null
    private var mAdapter: TestAutoDataAdapter? = null
    private var mMode = DragSelectionProcessor.Mode.ToggleAndUndo
    private var needSelect = false
    private var mDragSelectionProcessor: DragSelectionProcessor? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        datas.add(ItemBean(width = 2, height = 2))
        datas.add(ItemBean(width = 1, height = 1))
        datas.add(ItemBean(width = 2, height = 1))
        datas.add(ItemBean(width = 2, height = 1))
        datas.add(ItemBean(width = 1, height = 1))
        datas.add(ItemBean(width = 1, height = 1))
        datas.add(ItemBean(width = 2, height = 1))
        datas.add(ItemBean(width = 1, height = 1))
        datas.add(ItemBean(width = 1, height = 1))
        datas.addAll(datas)
        datas.addAll(datas)
        datas.addAll(datas)
        datas.addAll(datas)
//        datas.add(ItemBean(width = 2, height = 2))
        datas.addAll(datas)
//        datas.addAll(datas)
//        datas.addAll(datas)
//        datas.clear()
//        for(i in 1..100){
//            datas.add(ItemBean(width = (1..5).random(), height = (1..4).random()))
//        }
        var p11 = Point(1, 1)
        var p21 = Point(2, 1)
        var p22 = Point(2, 2)

        sureTv.setOnClickListener {
            Toast.makeText(this@MainActivity, "${mAdapter?.selection?.sorted()}", Toast.LENGTH_SHORT).show()
        }
        cancelTv.setOnClickListener {
            needSelect = false
            mAdapter!!.selection.clear()
            mAdapter!!.notifyDataSetChanged()
        }
        Log.d("MainActivity", "onCreate: ${datas.size}")
        mAdapter = TestAutoDataAdapter(this, datas.size)
        rlv.adapter = mAdapter

        var layoutManager =
            SpannedGridLayoutManager(SpannedGridLayoutManager.Orientation.VERTICAL, spans = 5)
//        layoutManager.isAutoMeasureEnabled =true
        rlv.layoutManager = layoutManager
        rlv.addItemDecoration(SpaceItemDecorator(left = 10, top = 10, right = 10, bottom = 10))

        layoutManager.spanSizeLookup = SpannedGridLayoutManager.SpanSizeLookup { pos ->
            SpanSize(datas[pos].width, datas[pos].height)
        }

        mAdapter!!.setClickListener(object : TestAutoDataAdapter.ItemClickListener {
            override fun onItemClick(view: View?, position: Int) {
                Log.d("dfsfsfs", "onItemClick: $position")
                if(!needSelect)
                mAdapter!!.toggleSelection(position)
            }

            override fun onItemLongClick(view: View?, position: Int): Boolean {
                // if one item is long pressed, we start the drag selection like following:
                // we just call this function and pass in the position of the first selected item
                // the selection processor does take care to update the positions selection mode correctly
                // and will correctly transform the touch events so that they can be directly applied to your adapter!!!
                mDragSelectTouchListener!!.startDragSelection(position)
                needSelect = true
                return true
            }

            override fun onItemTouch(view: View?, position: Int): Boolean {
                Log.d("dfsfsfs", "onItemTouch: $position")
                if (needSelect)
                    mDragSelectTouchListener!!.startDragSelection(position)
                return needSelect
            }
        })


        // 2) Add the DragSelectListener
        mDragSelectionProcessor =
            DragSelectionProcessor(object : DragSelectionProcessor.ISelectionHandler {
                override val selection: MutableSet<Int>
                    get() = mAdapter!!.selection


                override fun isSelected(index: Int): Boolean {
                    return mAdapter!!.selection.contains(index)
                }

                override fun updateSelection(
                    start: Int,
                    end: Int,
                    isSelected: Boolean,
                    calledFromOnStart: Boolean
                ) {
                    mAdapter!!.selectRange(start, end, isSelected)
                }
            })
                .withMode(mMode)
        mDragSelectTouchListener = DragSelectTouchListener()
            .withSelectListener(mDragSelectionProcessor)
        rlv.addOnItemTouchListener(mDragSelectTouchListener!!)
    }


}